/*
 * @Description: 档案备份计划上下文
 * @Author: Rfan
 * @Date: 2022-08-03 20:29:15
 * @LastEditTime: 2022-08-04 11:12:18
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import { Modal } from 'antd';
import { BackupReducer } from '../reducers/BackupReducer';
import {
  ADD_TRANSFER,
  DELETE_TRANSFER,
  EDIT_TRANSFER,
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  SET_TRANSFER_LIST
} from '../constants';
import { addTransfer, deleteTransfer, editTransfer, fetchTransferListByPage } from '@/api/transfer';

const initialState = {
  // 当前选中的项目
  transferState: 0,
  // 选中的表格条目
  selectItems: [],
  // 移交申请单列表
  transferList: []
};

const BackupContext = createContext();

const BackupContextProvider = (props) => {
  const [state, dispatch] = useReducer(BackupReducer, initialState);

  const actions = {
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    },

    /**
     * @description: 获取移交清单列表
     */
    getTransferList(params) {
      return new Promise((resolve, reject) => {
        fetchTransferListByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_TRANSFER_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },

    addTransferFunc(params) {
      return new Promise((resolve, reject) => {
        addTransfer(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: ADD_TRANSFER, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },

    editTransferFunc(params) {
      return new Promise((resolve, reject) => {
        editTransfer(params)
          .then((res) => {
            const { data } = res;
            const { id } = res.data;
            dispatch({ type: EDIT_TRANSFER, id, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },

    delTransferFunc(id, content = '确认删除选中的移交清单？') {
      Modal.confirm({
        title: '删除',
        content,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteTransfer(ids).then((res) => {
            const data = res.data.map((id) => parseInt(id, 10));
            dispatch({ type: DELETE_TRANSFER, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };

  // watch listQuery来加载排序列表
  useEffect(() => {}, []);

  return (
    <BackupContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </BackupContext.Provider>
  );
};

export const useBackupContext = () => {
  return useContext(BackupContext);
};

export { BackupContext, BackupContextProvider };
